let mysql = require('mysql')
let userModel = require('../model/userModel') // 用户model
// 引入生成token的模块
let jwt = require('jsonwebtoken')

function other (app) {
    app.post('/api/login', function (req, res) {
        // console.log(req.body)
        let { account, pswd } = req.body
        userModel.login(account, pswd, function (err, result) {
            // console.log('result------', result);
            if (err) { res.json({ success: false, data: err.message }) } else {
                if (result.length > 0) {
                    // jwt.sign(param，secret,option) param是加密的字符串 secret是加密的秘钥 option配置过期时间
                    let token = jwt.sign({ id: result[0].user_id }, '11111111112222323232321ddfsfdsaf', { expiresIn: 24 * 60 * 60 * 1000 })
                    res.json({ success: true, data: result[0], status: 0, message: '登录成功', token })
                } else {
                    res.json({ success: false, message: '用户名或密码错误' })
                }
            }

        })
    }),
        // 菜单
        app.get('/api/getmenu', async function (req, res) {
            let data = await userModel.getMenu()
            // console.log(data)
            const { err, result } = data
            if (err) {
                res.json({ success: false, message: err.message })
            } else {
                if (result.length > 0) {
                    res.json({ success: true, data: result, status: 0, message: 'menu ok' })
                } else {
                    res.json({ success: false, message: 'menu 未给出' })
                }
            }
        }),
        app.get('/api/getuserlist', async function (req, res) {
            console.log(req.query)
            const { page, pagesize } = req.query
            let data = await userModel.getUserList(page)
            const { err, result } = data
            console.log(result);
            if (err) {
                res.json({ success: false, message: err.message })
            } else {
                if (result.length > 0) {

                    let mapKey = [
                        { title: '用户Id', dataIndex: 'user_id', key: 'user_id' },
                        { title: '用户名', dataIndex: 'username', key: 'username' },
                        { title: '账号', dataIndex: 'account', key: 'account' },
                        { title: '密码', dataIndex: 'pswd', key: 'pswd' },
                        { title: '用户权限', dataIndex: 'type_id', key: 'type_id' }
                    ]
                    res.json({ success: true, data: { list: result, mapKey }, status: 0, message: 'list ok' })
                } else {
                    res.json({ success: false, message: 'list 未给出' })
                }
            }
        }),
        // 获取权限
        app.get('/api/getpower', async function (req, res) {
            let data = await userModel.getPower()
            const { err, result } = data
            if (err) {
                res.json({ success: false, message: err.message })
            } else {
                if (result.length > 0) {
                    res.json({ success: true, data: result, status: 0, message: 'menu ok' })
                } else {
                    res.json({ success: false, message: 'menu 未给出' })
                }
            }
        })
    // 新增用户
    app.post('/api/adduserinfo', async function (req, res) {
        let data = await userModel.addUserInfo(req.body)
        const { err, result } = data
        if (err) {
            res.json({ success: false, message: err.message })
        } else {
            res.json({ success: true, data: result, status: 0, message: 'menu ok' })
        }
    })
    // 根据user_id 获取用户信息
    app.get('/api/getuserinfo', async function (req, res) {
        let { user_id } = req.query
        let data = await userModel.getUserInfo(user_id)
        console.log(data)
        const { err, result } = data
        if (err) {
            res.json({ success: false, message: err.message })
        } else {
            if (result.length > 0) {
                res.json({ success: true, data: result[0], status: 0, message: '用户信息ok' })
            } else {
                res.json({ success: false, message: 'user未查出' })
            }
        }
    })
    // 编辑用户
    app.post('/api/edituserinfo', async function (req, res) {
        let data = await userModel.editUserInfo(req.body)
        const { err, result } = data
        if (err) {
            res.json({ success: false, message: err.message })
        } else {
            res.json({ success: true, data: result, status: 0, message: 'edit ok' })
        }
    })
    // 删除用户
    app.post('/api/deluser', async function (req, res) {
        // console.log(req.body)
        let data = await userModel.delUser(req.body.id)
        const { err, result } = data
        if (err) {
            res.json({ success: false, message: err.message })
        } else {
            res.json({ success: true, data: result, status: 0, message: 'edit ok' })
        }
    })
}
module.exports = other
